load('application');

before(loadBlog, {only: ['show', 'edit', 'update', 'destroy']});

action('new', function () {
    this.title = 'New blog';
    blog = new Blog;
    blog.content = "please enter world here is";
    this.blog = blog;
    render();
    //render('weibo');
});

action(function create() {
	var blog = new Blog;
	console.log('=============');
	console.log(req.body.content);
	blog.content = req.body.content;
	blog.authorName = 'jojo';
	blog.createAt = new Date;
	
	blog.userName = 'justin';
	Blog.create(blog,function (err, blog){
		if(err){
			flash('error','Blog can not be created');
		}
		else{
			flash('info', 'Blog created');
		}
	});
	blog.userName = 'jojo';
    Blog.create(blog, function (err, blog) {
        if (err) {
            flash('error', 'Blog can not be created');
            render('new', {
                blog: blog,
                title: 'New blog'
            });
        } else {
        	//console.log("test user start");
        	//User.testUser(User);
        	//console.log("test user end");
            flash('info', 'Blog created');
            redirect(path_to.blogs());
            //loadBlog();
            //render('weibo');
        }
    });
});

action(function index() {
    this.title = 'Blogs index';
    this.blog = new Blog;
    filter = {where:{'userName':'Justin'}};
    //filter = {where : {'content':'weibo '}};
    //filter.where = {'content':'weibo '};
    //console.log('filter:'+filter);
    //console.log('sss');
    Blog.all(filter,function (err, blogs) {
    	console.log(blogs);
        render({
            blogs: blogs,
            blog : this.blog
        });
    });
});

action(function show() {
    this.title = 'Blog show';
    render();
});

action(function edit() {
    this.title = 'Blog edit';
	console.log(this.blog.id);
	console.log(this.blog.content);
	var blog = new Blog;
	blog.authorName = this.blog.authorName;
	blog.content = this.blog.content;
	blog.userName = 'Justin';
	blog.createAt = new Date;
	var tempBlog = new Blog;
	tempBlog.userName = 'Justin';
	Blog.create(tempBlog, function (err, blog){
		if(err){
			flash('error','Blog can not be created');
		}
		else{
			flash('info', 'Blog created');
		});
	console.log('copy send');
	redirect('/blogs');
});

/*action('copysend',function copysend({
	console.log(this.blog.id);
	render('index');
}));*/

action(function update() {
    this.blog.updateAttributes(body.Blog, function (err) {
        if (!err) {
            flash('info', 'Blog updated');
            redirect(path_to.blog(this.blog));
        } else {
            flash('error', 'Blog can not be updated');
            this.title = 'Edit blog details';
            render('edit');
        }
    }.bind(this));
});

action(function destroy() {
	console.log("========destory here ==");
    this.blog.destroy(function (error) {
        if (error) {
            flash('error', 'Can not destroy blog');
        } else {
            flash('info', 'Blog successfully removed');
        }
        send("'" + path_to.blogs() + "'");
    });
});

function loadBlog() {
    Blog.find(params.id, function (err, blog) {
        if (err || !blog) {
            redirect(path_to.blogs());
        } else {
            this.blog = blog;
            next();
        }
    }.bind(this));
}
